package com.emergency.evaluate.service.impl;


import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.evaluate.dao.IndicatorDao;
import com.emergency.evaluate.dao.WeightDao;
import com.emergency.evaluate.domain.Indicator;
import com.emergency.evaluate.domain.Weight;
import com.emergency.evaluate.service.IndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/3/10.
 */
@Service
public class IndicatorServiceImpl implements IndicatorService {
    @Autowired
    private IndicatorDao indicatorDao;
    @Autowired
    private WeightDao weightDao;
    @Autowired
    private IdWorker idWorker;

    //通过指标类型查询指标,1为演练预案，2为指挥人员，3为救援人员，4为救援总体效果
    @Override
    public List<Indicator> findbykind(Integer kind){

        return indicatorDao.findAllByKindEquals ( kind );
    }


    //根据指标编号即id 删除指标
    @Override
   public void deleteById(Long id){ indicatorDao.deleteById (id);}

   // 保存指标
   @Override
   public List<Indicator> add(List<Indicator> indicatorList){
         for (Indicator indicator:indicatorList){
             if (CommonUtils.isNullOrEmpty(indicator.getId())) {  //如果指标ID 为空，自动赋值ID和创建时间
                 indicator.setId(idWorker.nextId());
                 indicator.setCreateTime(new Date ());
             }
             if(!CommonUtils.isNullOrEmpty(indicatorDao.findAllById ( indicator.getId () ))){
                 Indicator indicator1 = indicatorDao.findAllById ( indicator.getId () );
                 indicator.setCreateTime ( indicator1.getCreateTime () );
                 if(CommonUtils.isNullOrEmpty(indicator.getContext ())){
                     indicator.setContext ( indicator1.getContext () );
                 }
                 if(CommonUtils.isNullOrEmpty(indicator.getName ())){
                     indicator.setName ( indicator1.getName () );
                 }
                 if(CommonUtils.isNullOrEmpty(indicator.getKind ())){
                     indicator.setKind ( indicator1.getKind () );
                 }
                 if(CommonUtils.isNullOrEmpty(indicator.getStandard ())){
                     indicator.setStandard ( indicator1.getStandard () );
                 }
                 indicator.setModifyTime ( new Date (  ) );
             }
             indicatorDao.save (indicator);
         }
       return  indicatorList;
   }

   //分页功能
    @Override
    public  Page<Indicator> findList(Map searchMap, int page, int size){
        Specification<Indicator> specification = createSpecification(searchMap);
        PageRequest pageRequest =  PageRequest.of(page-1, size);
        return indicatorDao.findAll(specification, pageRequest);

     }
    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Indicator> createSpecification(Map searchMap) {
        return new Specification<Indicator>() {
            @Override
            public Predicate toPredicate(Root<Indicator> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate> ();

                // 根据指标名称查询
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(cb.like (root.get("name").as(String.class),"%"+ (String)searchMap.get ( "name" ) +"%"));
                }
                //根据指标类型查询
                if (searchMap.get("kind") != null && !"".equals(searchMap.get("kind"))) {
                    predicateList.add(cb.equal (root.get("kind").as(String.class), searchMap.get("kind")));
                }
                //根据评分方式查询，专家打分或系统打分
                if (searchMap.get("standard") != null && !"".equals(searchMap.get("standard"))) {
                    predicateList.add(cb.equal (root.get("standard").as(String.class), searchMap.get("standard")));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
    };

}


//系统预先录入权重或者修改
    public   List <Weight> addWeight(List <Weight> weightList){
       for (Weight weight:weightList){
           if (CommonUtils.isNullOrEmpty(weight.getId())) {  //如果指标ID 为空，自动赋值ID和创建时间
               weight.setId(idWorker.nextId());
               weight.setCreateTime(new Date ());
               weightDao.save ( weight );
           }
           if(!CommonUtils.isNullOrEmpty(weightDao.findAllById ( weight.getId () ))){
               Weight weight1 = weightDao.findAllById ( weight.getId () );
               weight.setCreateTime ( weight1.getCreateTime () );
               if(CommonUtils.isNullOrEmpty(weight.getIndicator ())){
                   weight.setIndicator ( weight1.getIndicator () );
               }
               if(CommonUtils.isNullOrEmpty(weight.getScenar ())){
                   weight.setScenar ( weight1.getScenar () );
               }
               weight.setModifyTime ( new Date (  ) );
               weightDao.save ( weight );
           }
       }
        return weightDao.findAll ();
    }

}
